from .model import FormattedMessageCategory, FormattedMessage
from transfer.server import server
from transfer.service import BaseService


class Service(BaseService):

    def category_get_all(self):
        return FormattedMessageCategory().get_all()

    def category_add_one(self, kwargs):
        return FormattedMessageCategory().add_one(**kwargs)

    def category_delete(self, name):
        msg = FormattedMessage().get(condition={'category':name})
        if not msg: # 该类别下没有消息时才可以删除
            return FormattedMessageCategory().delete({'name': name})
        return None

    def category_update(self, old, new):
        FormattedMessage().set({'category':old}, category=new)
        FormattedMessageCategory().set({'name': old}, name=new)

    def msg_get_all(self):
        return FormattedMessage().get_all()

    def msg_get(self, name):
        msg = FormattedMessage().get(condition={'name': name})
        if msg:
            return msg[0]
        else:
            return {}

    def msg_add_one(self, kwargs):
        return FormattedMessage().add_one(**kwargs)

    def msg_update(self, name, kwargs):
        return FormattedMessage().set({'name':name}, **kwargs)

    def msg_delete(self, name):
        return FormattedMessage().delete({'name':name})


if __name__ == "__main__":
    server.register_instance(Service())
    server.start()
